package org.traffometer.traffobase.webapp.control.command;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebTable;

import static org.junit.Assert.assertEquals;

import org.junit.Test; 

/**
 * Tests operation of the Traffobase Clear command.
 *
 * @author Jitender Miglani
 * @author Philip Johnson
 */
public class TestDisplayClearCommand {

  /** The stackMVC's only page title. */
  private static final String pageTitle = "Traffobase - Aqua";


  /** Get the test host. */
  private final String testUrl =  System.getProperty("test.host") +
      System.getProperty("traffobase.context.root") + "/";


  /**
   * Tests the Traffobase clear operation.
   *
   * @throws Exception If problems occur
   */
  @Test
  public void testClear() throws Exception {
    WebConversation conversation = new WebConversation();
    
    //  Get welcome.jsp page and check for successful retrieval
    String Url = testUrl + "/welcome.jsp";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking welcome.jsp retrieval", pageTitle, response.getTitle());
    
    // Go to the Traffobase page and check that we got it OK.
    // This tests Display command
    WebLink startLink = response.getLinkWithID("InitBase");
    response = conversation.getResponse(startLink.getRequest());
    assertEquals("Checking index.jsp retrieval", pageTitle, response.getTitle());
    WebTable packetStoreTable = response.getTableWithID("PacketStoreTable");
    assertEquals("Checking size of the test list of packets", 10, packetStoreTable.getRowCount()); 
    
    
    // Now try clearing the packet store
    WebForm clearForm = response.getFormWithID("ClearForm");
    response = conversation.getResponse(clearForm.getRequest());
    packetStoreTable = response.getTableWithID("PacketStoreTable");
    assertEquals("Checking size of an empty list of packets", 0, packetStoreTable.getRowCount());
  }
}
